/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.lczyfz.wisdom.modules.platform.service;

import com.lczyfz.wisdom.common.persistence.Page;
import com.lczyfz.wisdom.common.service.CrudService;
import com.lczyfz.wisdom.common.utils.ResponseCode;
import com.lczyfz.wisdom.common.utils.ServerResponse;
import com.lczyfz.wisdom.modules.platform.dao.PUserDao;
import com.lczyfz.wisdom.modules.platform.entity.PUser;
import com.lczyfz.wisdom.modules.platform.wxutils.CreateSignUtils;
import com.lczyfz.wisdom.modules.platform.wxutils.EncrUtil;
import com.lczyfz.wisdom.modules.platform.wxutils.SmsUtils;
import com.lczyfz.wisdom.modules.sys.utils.UserUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.SortedMap;
import java.util.TreeMap;

/**
 * puserService
 * @author css
 * @version 2019-09-26
 */
@Service
@Transactional(readOnly = true)
public class PUserService extends CrudService<PUserDao, PUser> {

	public PUser get(String id) {
		return super.get(id);
	}
	
	public List<PUser> findList(PUser pUser) {
		return super.findList(pUser);
	}
	
	public Page<PUser> findPage(Page<PUser> page, PUser pUser) {
		return super.findPage(page, pUser);
	}
	
	@Transactional(readOnly = false)
	public void save(PUser pUser) {
		super.save(pUser);
	}

	@Transactional(readOnly = false)
	public ServerResponse savePuser(PUser pUser) {

		if(null == pUser.getMobile() || "".equals(pUser.getMobile())){
			return new ServerResponse(-10000,"用户电话不能为空");
		}
		if(null == pUser.getMobile() || "".equals(pUser.getMobile())){
			return new ServerResponse(-10001,"用户密码不能为空");
		}
		if(null == pUser.getName() || "".equals(pUser.getName())){
			return new ServerResponse(-10002,"用户姓名不能为空");
		}
		if(null == pUser.getSign() || "".equals(pUser.getSign())){
			return new ServerResponse(-10003,"签名不能为空");
		}
		if(null == pUser.getVerifyCode() || "".equals(pUser.getVerifyCode())){
			return new ServerResponse(-10004,"验证码不能为空");
		}
		if(null == pUser.getStamp() || "".equals(pUser.getStamp())){
			return new ServerResponse(-10005,"时间戳不能为空");
		}
		long timeStamp = System.currentTimeMillis();
		SmsUtils smsUtils = new SmsUtils();
		String token = null;
		//短信验证码校验
		int msgCode = smsUtils.checkVerifyCode(pUser.getSign(),pUser.getVerifyCode(),pUser.getStamp()).getMsgCode();
		//短信校验成功
		if(msgCode == 0 && null != pUser){
			//验证用户是否注册
			PUser byMobile = UserUtils.getByMobile(pUser.getMobile());
			boolean result = (null == UserUtils.getByMobile(pUser.getMobile())) ? false:true;
			//已注册,生成token
			if(result){
				token = getToken(byMobile,timeStamp+"");
				return new ServerResponse(ResponseCode.SUCCESS.getCode(),"注册成功");
			}else {
				//注册并生成token
				super.save(pUser);
				token = getToken(pUser,timeStamp+"");
				return new ServerResponse(ResponseCode.SUCCESS.getCode(),"注册并且登录成功");
			}
		}else {
			return new ServerResponse(-10006,"短信验证失败,重新获取");
		}

		// TODO: 2019/9/30 判断用户信息是否已存在
//		    如果不存在，则注册

//		注册完成或者已存在直接登录

//		根据用户信息生成Token

//		返回用户Token

	}

	
	@Transactional(readOnly = false)
	public void delete(PUser pUser) {
		super.delete(pUser);
	}

	public String getToken(PUser pUser,String stamp){
		SortedMap<String, String> signParams = new TreeMap<String, String>();
		signParams.put("id",pUser.getId());
		signParams.put("stamp",stamp);
		String paramStr = CreateSignUtils.compileParams(signParams);
		String sign = CreateSignUtils.createSign("UTF-8",paramStr);
		return EncrUtil.encodeSrc(paramStr + "&sign=" + sign);
	}
	
}